package com.yxd.util;

import com.yxd.bootstrap.Constants;
import com.yxd.logging.SysLogger;
import com.yxd.vo.RequestLine;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;

/**
 * Created by Administrator on 2016/6/16.
 */
public class RequestInputStream extends InputStream {
    private final InputStream inputStream;
    private String[] requestData = null;

    public RequestInputStream(InputStream inputStream) {
        this.inputStream = inputStream;
    }

    private String[] _getRequestLines() {
        if (requestData != null) {
            return requestData;
        }

        ArrayList<String> arrayList = new ArrayList<>();
        try {
            InputStreamReader reader = new InputStreamReader(this.inputStream);
            BufferedReader bufferedReader = new BufferedReader(reader);
            String line = bufferedReader.readLine();

            while (line != null) {
                arrayList.add(line);
                if (line.equals("")) {
                    break;
                }
                line = bufferedReader.readLine();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return requestData = arrayList.toArray(new String[arrayList.size()]);
    }

    public RequestLine getRequestLine() {
        RequestLine requestLine = new RequestLine();
        try {
            String[] lines = _getRequestLines();
            if (lines != null && lines.length > 0) {
                String string = lines[0];
                int idx1 = string.indexOf(" ");
                if (idx1 >= 0) {
                    String method = string.substring(0, idx1);
                    int idx2 = string.indexOf(" ", idx1 + 1);
                    if (idx2 >= 0) {
                        String uri = string.substring(idx1 + 1, idx2);
                        int idx4 = uri.indexOf("?");
                        if (idx4 >= 0) {
                            uri = uri.substring(0, idx4);
                            requestLine.setQueryString(uri.substring(idx4 + 1));
                        }
                        String version = string.substring(idx2 + 1);
                        int idx3 = version.indexOf("/");
                        if (idx3 >= 0) {
                            requestLine.setProtocal(version.substring(0, idx3));
                            version = version.substring(idx3 + 1);
                        }
                        requestLine.setUri(uri);
                        requestLine.setVersion(version);
                    }
                    requestLine.setMethod(method);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return requestLine;
    }

    @Override
    public int read() throws IOException {
        return this.inputStream.read();
    }

    public HashMap<String, String> getRequestHeaders() {
        HashMap<String, String> headers = new HashMap<>();
        try {
            String[] lines = _getRequestLines();
            for (String line :
                    lines) {
                int idx1 = line.indexOf(":");
                if (idx1 > 0) {
                    String key = line.substring(0, idx1);
                    String value = line.substring(idx1 + 1);
                    headers.put(key, value);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return headers;
    }
}
